import Vue from 'vue'
import store from  '../store/index'
import VueRouter from 'vue-router'
import {Message} from "element-ui";

Vue.use(VueRouter)

const Login = () => import("@/views/login/Login");
const UserEdit = () => import("@/views/user/UserEdit");
const UserInfo = () => import("@/views/user/UserInfo");
const Blogs = () => import("@/views/blog/Blogs")
const BlogDetail = () => import("@/views/blog/BlogDetail");
const BlogEdit = () => import("@/views/blog/BLogEdit");
const About = () => import("@/components/blog/blogs/About");
const AboutDetail = () => import("@/views/blog/AboutDetail");
const AboutEdit = () => import("@/views/blog/AboutEdit");
const UserInfoEdit = () => import("@/views/user/UserInfo");
const UserAbout = () => import("@/views/user/UserAbout");
const Category = () => import("@/views/blog/Category");
const CategoryDeatil = () => import("@/views/blog/CategoryDetail");

const routes = [
  {
    path: "/",
    redirect: "/blogs"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/user",
    redirect: "/user/blog/edit",
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/info",
    component: UserInfo,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/blog/edit",
    component: UserEdit,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/blog/add",
    component: BlogEdit,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/blog/:blogId",
    component: UserEdit,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/blogs",
    component: Blogs
  },
  {
    path: "/blog/about",
    component: AboutDetail
  },
  {
    path: "/blog/category",
    component: Category
  },
  {
    path: "/blog/category/:category",
    component: CategoryDeatil
  },
  {
    path: "/blog/:blogId",
    component: BlogDetail
  },
  {
    path: "/user/blog/:blogId/edit",
    component: BlogEdit,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/about",
    component: About
  },
  {
    path: "/user/about",
    component: UserAbout,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/about/edit",
    component: AboutEdit,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/user/info/edit",
    component: UserInfoEdit,
    meta: {
      requireAuth: true
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if(to.matched[0].meta.requireAuth) {
    if(store.getters.isLogin) {
      next();
    }else {
      Message.warning("请先进行登录！")
      next({
        path: "/login"
      })
    }
  }else {
    next();
  }
})

export default router
